package cn.edu.gzhmt.user.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.edu.gzhmt.user.dao.IUserDao;
import cn.edu.gzhmt.user.domain.User;
import cn.edu.gzhmt.utils.jdbc.TxQueryRunner;

/**
 * User持久层实现类
 * 使用apache提供的小工具
 * @author azhuo
 *
 */
public class UserDaoImpl implements IUserDao {

	private QueryRunner queryRunner = new TxQueryRunner();

	/**
	 * 添加用户
	 */
	public void addUser(User user) {
		try {
			// 创建sql模板
			String sql = "INSERT INTO `t_user`(`uid`,`username`,`password`,`phone`,`email`,`address`,`code`,`state`,`isstate`) VALUES ( ?,?,?,?,?,?,?,?,?)";

			// 设置参数
			Object[] params = { user.getUid(), user.getUsername(),
					user.getPassword(), user.getPhone(), user.getEmail(),
					user.getAddress(), user.getCode(), user.getState(),
					user.getIsstate() };

			// 插入记录
			queryRunner.update(sql, params);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 根据激活码查询客户
	 */
	public User findUserByStateCode(String stateCode) {
		try {
			String sql = "SELECT * FROM t_user WHERE state=?";

			// 进行查询
			User user = queryRunner.query(sql,
					new BeanHandler<User>(User.class), stateCode);

			return user;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

	/**
	 * 根据用户id激活用户
	 */
	public void updateState(String uid, boolean state) {
		try {
			String sql = "UPDATE t_user SET isstate=? WHERE uid=?";

			Object[] params = { state, uid };

			queryRunner.update(sql, params);

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

	/**
	 * 根据用户名查询用户
	 */
	public User findUserByUsername(String username) {
		try {
			String sql = "SELECT * FROM t_user WHERE username=?";

			User user = queryRunner.query(sql,
					new BeanHandler<User>(User.class), username);

			return user;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 通过邮箱查询用户
	 */
	public User findUserByEmail(String email) {
		try {
			String sql = "SELECT * FROM t_user WHERE email=?";

			User user = queryRunner.query(sql,
					new BeanHandler<User>(User.class), email);

			return user;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 根据用户id，字段名称，字段值来更新用户信息
	 */
	public void update(String uid, String columnName, String columnValue) {
		try {
			String sql = "UPDATE t_user SET " + columnName + "=? WHERE uid=?";
			Object[] params = { columnValue, uid };
			queryRunner.update(sql, params);

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 根据用户id查询用户
	 */
	public User findUserById(String uid) {
		try {
			String sql = "SELECT * FROM t_user WHERE uid=?";

			User user = queryRunner.query(sql,
					new BeanHandler<User>(User.class), uid);

			return user;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 查看所有用户
	 */
	public List<User> findAll() {
		try {
			String sql = "SELECT * FROM t_user WHERE `isadmin`='0' AND `udel`=0";

			return queryRunner
					.query(sql, new BeanListHandler<User>(User.class));

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 查看所有管理员
	 */
	public List<User> findAllAdmin() {
		try {
			String sql = "SELECT * FROM t_user WHERE `isadmin`='1' AND `udel`=0";

			return queryRunner
					.query(sql, new BeanListHandler<User>(User.class));

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 使用模糊查询用户
	 */
	public List<User> findUserByUsernameLike(String username) {
		try {
			String sql = "SELECT * FROM t_user WHERE username LIKE ? AND `udel`=0 AND `isadmin`='0'";

			List<User> userList = queryRunner
					.query(sql, new BeanListHandler<User>(User.class), "%"
							+ username + "%");

			return userList;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 用户名查询管理员用户
	 */
	public List<User> findAdminUserByUsernameLike(String username) {
		try {
			String sql = "SELECT * FROM t_user WHERE username LIKE ? AND `udel`=0 AND `isadmin`='1'";

			List<User> userList = queryRunner
					.query(sql, new BeanListHandler<User>(User.class), "%"
							+ username + "%");

			return userList;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

}
